class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        s_len = len(s)
        if s_len < 1:
            return 0
        result = 1
        head, tail = 0, 1
        while tail < s_len:
            while tail < s_len and s[tail] not in s[head:tail]:
                tail += 1
            tmp = tail - head
            if tmp > result:
                result = tmp
            if tail < s_len and s[tail] in s[head:tail]:
                if tail+1 < s_len and s[tail] == s[tail+1]:
                    while tail+1 < s_len and s[tail] == s[tail+1]:
                        tail += 1
                    tail += 1
                    head = tail - 1
                else:
                    head = s[head:tail].index(s[tail]) + head + 1
                    tail += 1
        return result


so = Solution()
s = "abcabcbb"
result = so.lengthOfLongestSubstring(s)
print(result)

s = "bbbbb"
result = so.lengthOfLongestSubstring(s)
print(result)

s = "pwwkew"
result = so.lengthOfLongestSubstring(s)
print(result)
